<template>
	<div>
		<el-dialog   title="账号添加" v-model="show" @close="close()" :destroy-on-close="true" custom-class="dia" :center="true" >
			<FormAttr :config="form" :params="data.params" :rules="data.rules" ref="validate" />
			<template #footer>
				<el-button @click="close()" type="warning">取消</el-button>
			<el-button @click="save()" type="primary">保存</el-button>
			</template>
		</el-dialog>	
		
	</div>
</template>

<script>
	import {
		reactive,
		toRef,
		ref,
		computed,
		onMounted,
		watch

	} from 'vue';
	import FormAttr from '/@/components/from/FormAttr.vue'
	import {
		existsAdmin,
		saveUserAdmin
	} from '/@/api/system.js'
	export default {
		components: {
			FormAttr
		},
		props: {
			prepend: {
				type: String,
				default: ''
			},
			uid: {
				type: Number,
				default: 0
			},
			show:{
				type:Boolean,
				default:false
			},
			type:{
				type:String,
				default:''
			},
			level:{
				type:Number,
				default:1 
			}
		},
		setup(props, {
			emit
		}) {
			const validate = ref(null)
			const p = ref("")
			const form = ref(null)
			const data = reactive({
				form: [{
					field: 'account',
					name: '账号',
					model: 'input',
					prepend: p
				}, {
					field: 'pwd',
					name: '密码',
					model: 'input',
					type: 'password'
				}, {
					field: 'epwd',
					name: '确认密码',
					model: 'input',
					type: 'password'
				}],
				form1: [{
					field: 'account',
					name: '账号',
					model: 'input',
					prepend: p
				}, {
					field: 'opwd',
					name: '原密码',
					model: 'input',
					type: 'password'
				}, {
					field: 'pwd',
					name: '密码',
					model: 'input',
					type: 'password'
				}, {
					field: 'epwd',
					name: '确认密码',
					model: 'input',
					type: 'password'
				}],
				params: {},
				rules: {
					account: [{
							required: true,
							message: '请输入账号',
							trigger: 'blur'
						},
						{
							min: 6,
							max: 16,
							message: '长度在 6 到 16 个字符',
							trigger: 'blur'
						}
					],
					pwd: [{
							required: true,
							message: '请输入密码',
							trigger: 'blur'
						},
						{
							min: 6,
							max: 16,
							message: '长度在 6 到 16 个字符',
							trigger: 'blur'
						}
					],
					epwd: [{
							required: true,
							message: '请确认密码',
							trigger: 'blur'
						},
						{
							min: 6,
							max: 16,
							message: '长度在 6 到 16 个字符',
							trigger: 'blur'
						}
					],
					opwd: [{
							required: true,
							message: '请输入原密码',
							trigger: 'blur'
						},
						{
							min: 6,
							max: 16,
							message: '长度在 6 到 16 个字符',
							trigger: 'blur'
						}
					],
				}
			})
			onMounted(() => {
				
			})
			
			watch(()=>props.show,(nld,old)=>{
			
				
				if( nld && validate.value!=null)
			    {
					validate.value.resetFields()
				}
				if(nld){
					p.value = props.prepend
					existsAdmin({
						uid: props.uid,
						level:props.level+1
					}).then(res => {
						if (res.data)
							form.value = data.form1
						else
							form.value = data.form
					})
				}
			})
			
			const save =()=>{
				validate.value.validate((valid)=>{
					if(valid){
						let p ={
							account:props.prepend+data.params.account,
							uid:props.uid,
							pwd:data.params.pwd,
							epwd:data.params.epwd,
							opwd:data.params.opwd,
							type:props.type,
							level:props.level+1
						}
					
						console.log('res>>>>>>para,>>>',JSON.stringify(p))
						saveUserAdmin(p).then(res=>{
							data.params={}
							close()
						})
					}
				})
			}
			const close = () => {
				emit("update:show", false);
				return true;
			}
			return {
				data,
				validate,
				form,
				save,
				close
			}
		}
	}
</script>

<style>
</style>
